package cn.sinjet.carassist;


import com.mgroup.wazehelper.R;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

public class FloatMenu {
	public static String tag = "FloatMenu";
	Context context;
	private View mFloatView = null;
	private WindowManager mWindowManager;
	private WindowManager.LayoutParams mLayoutParams;
	private int mCurrentX = -960;
	private int mCurrentY = -960;
	private int mFloatViewWidth = 60;
	private int mFloatViewHeight = 60;
	 int screenWidth =  1024 ;
     int screenHeight = 960 ;
	boolean isShown = false;
	OnFloatMenuClickListener onClickListener = null;
	ImageView mImg;
	private int orientation = 2;
	public FloatMenu(Context context){
		this.context = context;
		mWindowManager = (WindowManager)context.getApplicationContext().getSystemService(context.WINDOW_SERVICE);
		
	}
	public interface OnFloatMenuClickListener{
		void onFloatMenuClick(View v,int x,int y);
	}
	//public createFloatMenu();
	public void setOnFloatMenuClickListener(OnFloatMenuClickListener onClickListener){
		this.onClickListener = onClickListener;
	}
	private void createFloatMenu(){
		if(mFloatView != null)
			return;
        mFloatView = (View)LayoutInflater.from(context).inflate(R.layout.float_menu, null);
        mImg = (ImageView)mFloatView.findViewById(R.id.float_menu_image);
         mLayoutParams = new WindowManager.LayoutParams();
       
         mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        mLayoutParams.format = PixelFormat.RGBA_8888;
        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
//        if(mWindowManager.getDefaultDisplay().getOrientation() == 1)
        DisplayMetrics dm = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(dm);
//        int  Phone_width  = dm.widthPixels ;
//        int Phone_height = dm.heightPixels ;
        screenWidth =   dm.widthPixels ;
        screenHeight = dm.heightPixels ;
        Log.d(tag, "screen width:"+screenWidth+"height:"+screenHeight);
        mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        if(screenWidth >= screenHeight){
        	mLayoutParams.x = 0;//screenWidth/6;
        	if(mCurrentY == -960) mLayoutParams.y = screenHeight/2-mFloatViewHeight/2;
        	else mLayoutParams.y = mCurrentY;
        } else {
        	if(mCurrentX == -960) mLayoutParams.x = screenWidth/2-mFloatViewWidth/2;
        	else mLayoutParams.x = mCurrentX;
        	mLayoutParams.y = 0;
        }
        
        mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;//screenWidth/6;
        mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;//80screenHeight/8;
        mWindowManager.addView(mFloatView, mLayoutParams);
        isShown = true;
        mFloatView.setOnTouchListener(new OnFloatViewTouchListener(SinjetApplication.getInstance()));
        
       /* mFloatView.findViewById(R.id.drag_image).setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				mFloatView.findViewById(R.id.drag_image).setOnClickListener(null);
				mFloatView.setOnTouchListener(new OnFloatViewTouchListener());
//				OsdMenu osdMenu = new OsdMenu(SinjetApplication.getInstance());
				return true;
			}
		});*/
        /*mFloatView.findViewById(R.id.drag_image).setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			Log.d(tag, "screen");
//			ViewModel.getIns().jumpPageInUIThread(PageConstant.PAGE_HOME);
			 Intent appIntent = new Intent(Intent.ACTION_MAIN);  
	        appIntent.setAction(Intent.ACTION_MAIN);  
	        appIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
	        appIntent.setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
	        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步，设置启动模式  
		    SinjetApplication.getInstance().startActivity(appIntent);    
		}
	});*/
	}
	
/*	public void setImage(int res){
		mFloatView.setBackgroundResource(res);
	}*/
	
	
    private class OnFloatViewTouchListener implements View.OnTouchListener{
    	
//    	private Vibrator vibrator;
    	boolean isMoving = false;

    	Handler handler = new Handler();
        public OnFloatViewTouchListener(Context context){
//        	vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
    	}
    	Runnable pressRunnable = new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
//				vibrator.vibrate(100);
				isMoving = true;
			}
		};
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
//            mCurrentY = (int) event.getRawY()-mFloatViewHeight/2;
//            Log.i(tag, "mCurrentY: " + mCurrentY);
//            Log.i(tag, "rawx: " + mCurrentY);
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
           	   Log.d(tag,"floatView event down");
           	   isMoving = false;
           	   mImg.setImageResource(R.drawable.float_menu_d);
                break;
            case MotionEvent.ACTION_MOVE:
                updateFloatView(event.getRawX(),event.getRawY());
                if(!isMoving)
            	  handler.postDelayed(pressRunnable,200);
                break;
            case MotionEvent.ACTION_UP:
           	    Log.d(tag,"floatView event up");
           	    mImg.setImageResource(R.drawable.float_menu_u);
                handler.removeCallbacks(pressRunnable);
                if(!isMoving){
           		      Log.d(tag,"floatView onclicked");
           		    FloatMenu.this.onClick();    
           		 }
                break;
            }
            return true;
        }
    }
    private void onClick(){
    	Intent appIntent = new Intent(Intent.ACTION_MAIN);  
        appIntent.setAction(Intent.ACTION_MAIN);  
        appIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
        appIntent.setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步，设置启动模式
//        appIntent.putExtra("GoFirstPage", true); 
	    SinjetApplication.getInstance().startActivity(appIntent);
    }
    public void removeView(){
    	if(mFloatView !=null){
    		mWindowManager.removeView(mFloatView);
    		mFloatView = null;
    	}
    }
    private void updateFloatView(float x, float y) {
    	 if(orientation == 2){
	        	mLayoutParams.x = 0;//screenWidth/6;
	        	mLayoutParams.y = mCurrentY = (int)y-mFloatViewHeight/2;
	        }else if(orientation == 1){
	        	mLayoutParams.x = mCurrentX = (int)x-mFloatViewWidth/2;
	        	mLayoutParams.y = 0;
	     }
        mWindowManager.updateViewLayout(mFloatView, mLayoutParams);
    }
	public void show() {
		// TODO Auto-generated method stub
		if(isShown) return;
		createFloatMenu();
	}
	public void hide(){
		if(mFloatView != null && isShown){
			mWindowManager.removeView(mFloatView);
			isShown = false;
			mFloatView = null;
		}
	}
	
	public boolean isShown(){
		return isShown;
	}
	public void onOrientationChanged(int orientation) {
		// TODO Auto-generated method stub
		this.orientation  = orientation;
		if(!isShown) return;
		DisplayMetrics dm = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(dm);
        screenWidth =   dm.widthPixels ;
        screenHeight = dm.heightPixels ;
		 /*if(orientation == 2)
	        	mLayoutParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
	     else if(orientation == 1)
	    	 mLayoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;*/
		 if(orientation == 2){
			    mLayoutParams.x = 0;//screenWidth/6;
	        	if(mCurrentY == -960) mLayoutParams.y = screenHeight/2-mFloatViewHeight/2;
	        	else mLayoutParams.y = mCurrentY;
	        }else if(orientation == 1){
	        	if(mCurrentX == -960) mLayoutParams.x = screenWidth/2-mFloatViewWidth/2;
	        	else mLayoutParams.x = mCurrentX;
	        	mLayoutParams.y = 0;//
	     }
	     mWindowManager.updateViewLayout(mFloatView, mLayoutParams);
	}
    
}
